//
//  CLPictorialFocusCell.m
//  chanlin
//
//  Created by shuhui on 15-6-24.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLPictorialFocusCell.h"
#import "UIView+Utils.h"
#import "UIImageView+WebCache.h"

#define IFIndexFocusImgHeight  180.0f
#define IFIndexFocusImgWidth   320.0f

@interface CLPictorialFocusCell ()

@property (nonatomic, strong) UIImageView *titlePic;
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UILabel *focusTitle;
@property (nonatomic, strong) UIImageView *commentIcon;
@property (nonatomic, strong) UILabel *commentLab;



@property (nonatomic,strong)IfengCycleScrollView *cycleFocusView;//循环滚动焦点view
@property (nonatomic,strong)NSMutableArray *slideImgViewArray;//保存焦点图imageView


@end

@implementation CLPictorialFocusCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
        
    }
    return self;
}

- (void)createView{
    
    //图
    if (!self.titlePic) {
        self.titlePic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, GlobleWidth, 180)];
        self.titlePic.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.titlePic];
    }
    
    //莲花
    if (!self.iconImg) {
        self.iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, self.titlePic.bottom+12, 24, 16)];
        [self.iconImg setImage:[UIImage imageNamed:@"pictorial_lotus.png"]];
        [self addSubview:self.iconImg];
    }
    
    //标题
    if (!self.focusTitle) {
        self.focusTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.iconImg.right+8, self.titlePic.bottom+11, GlobleWidth-55, 18)];
        self.focusTitle.backgroundColor = [UIColor clearColor];
        self.focusTitle.textColor = RGB(0x56, 0x56, 0x5e);
        self.focusTitle.font = [UIFont systemFontOfSize:18.f];
        self.focusTitle.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:self.focusTitle];
    }
    
    //评论icon
    if (!self.commentIcon) {
        self.commentIcon = [[UIImageView alloc] initWithFrame:CGRectMake(GlobleWidth-47, self.titlePic.bottom+16, 12, 12)];
        self.commentIcon.backgroundColor = [UIColor clearColor];
        [self.commentIcon setImage:[UIImage imageNamed:@"index_commentCount_icon.png"]];
        [self addSubview:self.commentIcon];
    }
    
    //评论数
    if (!self.commentLab) {
        self.commentLab = [[UILabel alloc]initWithFrame:CGRectMake(self.commentIcon.right+4, self.commentIcon.top, 25, 10)];
        self.commentLab.backgroundColor = [UIColor clearColor];
        self.commentLab.textColor = RGB(0x5b, 0x85, 0x9e);
        self.commentLab.font = [UIFont systemFontOfSize:10.f];
        self.commentLab.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:self.commentLab];
    }
    
    //分割线
    UIImageView *seprateLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 220-0.5, GlobleWidth, 0.5)];
    seprateLine.backgroundColor = RGB(0xe0, 0xe0, 0xe0);
    [self addSubview:seprateLine];
    
    
    _cycleFocusView = [[IfengCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight)];
    _cycleFocusView.delegate = self;
    _cycleFocusView.datasource = self;
    [self addSubview:_cycleFocusView];
    
    [self addGesture];
}

- (void)loadData:(CLList *)newsList{
    [self reloadCell:0 indexList:newsList];
}

- (void)addGesture
{
    UITapGestureRecognizer *aRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
    aRec.numberOfTapsRequired = 1;
    aRec.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:aRec];
    
}

- (void)didTap:(UITapGestureRecognizer *)aRec
{
    if(self._blkTapResponse)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        self._blkTapResponse(self.indexList,self.currentIndex,indexPath);
        
    }
}

-(int)fitCountForCycleScrollView{//当条数为2时特殊处理
    return (2==[self photoCount])?4:[self photoCount];
}
-(int)fitIndexForCycleScrollView:(int)originalIndex{//当条数为2时特殊处理
    return (2==[self photoCount])?(originalIndex%2):originalIndex;
}

- (void)reloadCell:(int)acurrentIndex indexList:(CLList *)indexList{

    self.indexList = indexList;
    [self reload];
}

- (void)loadDataAtIndex:(int)index
{
    
    //设置标题
    NSString *title = [self subIndexListForIndex:index].title;
    if(_isStrNULL(title)){
        self.focusTitle.hidden = YES;
        self.iconImg.hidden = YES;
        self.focusTitle.text = @"";
    }else{
        self.focusTitle.hidden = NO;
        self.iconImg.hidden = NO;
        self.focusTitle.text = title;
    }
    
    [self.titlePic sd_setImageWithURL:[NSURL URLWithString:[self subIndexListForIndex:index].thumnail]
                     placeholderImage:[UIImage imageNamed:@"index_focus_default.png"]];

    if ([self subIndexListForIndex:index].comment && [self subIndexListForIndex:index].comment>0) {
        self.commentIcon.hidden = NO;
        self.commentLab.hidden =NO;
        self.commentLab.text = [NSString stringWithFormat:@"%d",[self subIndexListForIndex:index].comment];
    }else{
        self.commentIcon.hidden = YES;
        self.commentLab.hidden =YES;
    }
    
}

- (CLList *)subIndexListForIndex:(int)index{
    CLList *subIndexList = [self.indexList.focus sgrOrderSetObjectAtIndex:index];
    if(subIndexList && [subIndexList isKindOfClass:[CLList class]]){
        return subIndexList;
    }
    return nil;
}

- (int)photoCount{
    return [self.indexList.focus count];
}

- (void)reload{
    
    [self.cycleFocusView reloadData];
}

#pragma -
#pragma mark -CycleScrollViewDelegate,CycleScrollViewDatasource

- (NSInteger)cSGetNumberOfPages
{
    if (!_slideImgViewArray) {
        int count = [self fitCountForCycleScrollView];
        _slideImgViewArray = [[NSMutableArray alloc] initWithCapacity:count];
        for (int i=0; i<count; i++) {
            UIImageView *slideImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight)];
            [_slideImgViewArray sgrAddObject:slideImage];
            slideImage.backgroundColor= [UIColor clearColor];
            
        }
    }
    
    return [_slideImgViewArray count];
}

- (UIView *)cSGetPageViewAtIndex:(NSInteger)index
{
    int _index = [self fitIndexForCycleScrollView:index];
    UIImageView *slideImageV=[_slideImgViewArray sgrGetType:[UIImageView class] forIndex:index];
    NSString *str=[self subIndexListForIndex:_index ].thumnail;
    if(_isStrNotNull(str)){
        [slideImageV sd_setImageWithURL:[NSURL URLWithString:str]
                       placeholderImage:[UIImage imageNamed:@"index_focus_default.png"]];
    }else{
        slideImageV.image=[UIImage imageNamed:@"index_focus_default.png"];
    }
    
    UIView *IconView = [slideImageV viewWithTag:333];
    if(!IconView){
        UIImageView *playIcon=[[UIImageView alloc] init];
        playIcon.image=[UIImage imageNamed:@"doc_playImage.png"];
        playIcon.tag=333;
        playIcon.frame=CGRectMake((slideImageV.width-48.0f)/2.0f,
                                  (slideImageV.height-48.0f)/2.0f, 48.0f, 48.0f);
        
        IconView = playIcon;
        [slideImageV addSubview:playIcon];
    }
    IconView.hidden = !(((CLList *)[self.indexList.focus sgrOrderSetObjectAtIndex:index%[self.indexList.focus count]]).link==CLList_video);

    return slideImageV;
}


- (void)cSLoadDataAtIndex:(int)index
{
    int _index = [self fitIndexForCycleScrollView:index];
    self.currentIndex=_index;
    
    //设置标题
    NSString *title = [self subIndexListForIndex:_index].title;
    if(_isStrNULL(title)){
        self.focusTitle.hidden = YES;
        self.iconImg.hidden = YES;
        self.focusTitle.text = @"";
    }else{
        self.focusTitle.hidden = NO;
        self.iconImg.hidden = NO;
        self.focusTitle.text = title;
    }
    
    [self.titlePic sd_setImageWithURL:[NSURL URLWithString:[self subIndexListForIndex:_index].thumnail]
                     placeholderImage:[UIImage imageNamed:@"index_focus_default.png"]];
    
    if ([self subIndexListForIndex:_index].comment && [self subIndexListForIndex:_index].comment>0) {
        self.commentIcon.hidden = NO;
        self.commentLab.hidden =NO;
        self.commentLab.text = [NSString stringWithFormat:@"%d",[self subIndexListForIndex:_index].comment];
    }else{
        self.commentIcon.hidden = YES;
        self.commentLab.hidden =YES;
    }
    
    
    
}

@end
